package com.isoft.controll;

import com.isoft.menu.Menu;
import com.isoft.pojo.Goods;
import com.isoft.service.GoodsService;
import com.isoft.tools.Tools;

import java.util.Scanner;

public class GoodsController {
    Scanner scanner;//输入流
    //Alt+insert   (Alt+fn+insert)
    GoodsService goodsService;//声明服务层对象

    public GoodsController() {
        scanner = new Scanner(System.in);//实例化对象
        goodsService = new GoodsService();//实例化对象
    }

    public void startSystem() {
        try {
            Goods goods;//声明一个实体对象
            String goodsId = "";//运行时输入
            do {
                Menu.sysMenu();
                String userSelect = scanner.next();
                switch (userSelect) {
                    case "1":
                        goods = new Goods();
                        System.out.print("请输入商品ID:");
                        String id = scanner.next();
                        System.out.print("请输入商品名称:");
                        String goodsName = scanner.next();
                        System.out.print("请输入商品类型:");
                        String goodsType = scanner.next();
                        System.out.print("请输入商品库存:");
                        int inventory = scanner.nextInt();
                        System.out.print("请输入商品单价:");
                        double price = scanner.nextDouble();
                        goods.setGoodsId(id);
                        goods.setGoodsName(goodsName);
                        goods.setGoodsType(goodsType);
                        goods.setInventory(inventory);
                        goods.setPrice(price);
                        goodsService.addGoods(goods);
                        break;
                    case "2":
                        System.out.print("请输入要删除的商品ID:");
                        goodsId = scanner.next();
                        goodsService.deleteGoods(goodsId);
                        break;
                    case "3":
                        System.out.print("请输入要修改商品的ID：");
                        goodsId=scanner.next();
                        Goods goods1 = goodsService.findGoodsByIDForUpdate(goodsId);
                        if (goods1==null){
                            System.out.println("查无此商品");
                        }else{
                            System.out.println("正在执行修改商品功能...");
                            System.out.println("----------原商品信息------------");
                            System.out.println(goods1);
                            System.out.println("------------------------------");
                            System.out.print("请输入商品名称:");
                             goodsName = scanner.next();
                            System.out.print("请输入商品类型:");
                             goodsType = scanner.next();
                            System.out.print("请输入商品库存:");
                             inventory = scanner.nextInt();
                            System.out.print("请输入商品单价:");
                             price = scanner.nextDouble();
                             goods1.setGoodsName(goodsName);
                             goods1.setGoodsType(goodsType);
                             goods1.setInventory(inventory);
                             goods1.setPrice(price);
                             //找服务层，要更新商品的方法
                            goodsService.updateGoods(goods1);
                        }
                        break;
                    case "4":
                        System.out.print("请输入要查询的商品ID:");
                        goodsId = scanner.next();
                        goodsService.findGoodsByID(goodsId);
                        break;
                    case "5":
                        exit();
                        break;
                }
                System.out.println("按任意键继续...");
                System.in.read();//阻塞方法,等待用户输入
                Tools.clearScreen();//调用清屏的方法
            } while (true);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("系统出错了，请重试");
        }//Ctrl+Alt+L格式化代码

    }

    public void exit() {
        System.out.print("是否真的要退出本系统?(Y|N):");
        String flag=scanner.next();
        if (flag.equalsIgnoreCase("y")){
            System.exit(0);
        }
    }
}
